#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QImage image1;
    image1.load(":/img/backgroud.jpg");
    QImage image2=image1.scaled(1024,600);

    QPalette palette;
    palette.setBrush(this->backgroundRole(),QBrush(image2));//设置背景
    setPalette(palette);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::find(int id)
{
    this->id = id;
    User *u = UserList::userlist->at(id);
    QFont ft;
    ft.setPointSize(16);
    ui->labName->setFont(ft);
    ui->labNumber->setFont(ft);
    ui->labwinNumber->setFont(ft);
    ui->labName->clear();
    ui->labNumber->clear();
    ui->labwinNumber->clear();
    ui->labName->setText(QString::fromStdString(u->name));
    ui->labNumber->setText(QString::fromStdString(u->number));
    ui->labwinNumber->setText(QString::fromStdString(u->winNumber));
}

void MainWindow::on_btnPeople_clicked()
{
    if(id < 0)
    {
        QMessageBox::warning(this,"提示","请先登录！");
        return;
    }
    //自由模式
    Game *g=new Game();
    g_gameSettings.setGameType(2);
    g->setWindowModality(Qt::ApplicationModal);
    g->show();
}

void MainWindow::on_actionSignOut_triggered()
{
    if(id < 0)
    {
        QMessageBox::warning(this,"提示","请先登录！");
        return;
    }
    if(QMessageBox::information(this,"退出登录","是否退出登录？",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes)==QMessageBox::Yes){
        clearInfo();
        id = -1;
    }
}

void MainWindow::on_actionLogin_triggered()
{
    if(id != -1){
        QMessageBox::warning(this,"提示","你已经登录！");
        return;
    }
    frmLogin l;// 登录界面
    l.setWindowModality(Qt::ApplicationModal);
    if(l.exec() == QDialog::Accepted)//判断accept()函数
       {
           id = l.getId();
           find(id);
       }
       else
       {
           id = -1;
           clearInfo();
       }
}

void MainWindow::on_btnAi_clicked()
{
    if(id < 0)
    {
        QMessageBox::warning(this,"提示","请先登录！");
        return;
    }
    //人机对战
    //游戏总对局数加1；
    int one = atoi(UserList::userlist->at(id)->number.c_str()) + 1;
    UserList::userlist->at(id)->number =  to_string(one);
    Game *ag = new Game();
    g_gameSettings.setGameType(1);
    ag->setWindowModality(Qt::ApplicationModal);
    ag->show();
    //如果赢了 胜局+1
    if(ag->win){
        int one2 = atoi(UserList::userlist->at(id)->winNumber.c_str()) + 1;
        UserList::userlist->at(id)->winNumber =  to_string(one2);
    }
    find(id);
}

void MainWindow::on_actionUserInfo_triggered()
{
    if(id < 0)
    {
        QMessageBox::warning(this,"提示","请先登录！");
        return;
    }
    frmUserInfo *uo = new frmUserInfo();
    uo->setId(id);
    uo->setWindowModality(Qt::ApplicationModal);
    uo->exec();
    id = uo->getId();
    if(id < 0){
        clearInfo();
        QMessageBox::warning(this,"提示","用户已退出！");
    }
}

//清除信息
void MainWindow::clearInfo()
{
    ui->labName->setText("");
    ui->labNumber->setText("");
    ui->labwinNumber->setText("");
}

void MainWindow::on_btnRanking_clicked()
{
    // 排行榜，无需判断用户是否登录
    ranking_List *rl = new ranking_List();
    rl->show();
}

void MainWindow::on_actionAbout_triggered()
{
    frmAbout *a = new frmAbout();
    a->setWindowModality(Qt::ApplicationModal);
    a->show();
}
